# -*- coding: utf-8 -*-
# @Author : y
# @Date   : 2021/6/15
# @File   : EG37_EntProvidentFundOweCnt.py
# @E-mail : yantao@bbdservice.com

'''eof
name:申请企业公积金欠缴期数
code:EG37_EntProvidentFundOweCnt
tableName:
columnName:
groups:企业通用模块
dependencies:FGW_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''
import sys
import pandas as pd
import json
from datetime import datetime


def format_data(tableName):
    try:
        eg = FGW_DSJ.get("data").get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]

def get_date_list():
    try:
        now = datetime.now()
        aaa = [(now + pd.tseries.offsets.DateOffset(months=i)).strftime('%Y%m%d') for i in [-36, 0]]
        return aaa
    except:
        return None


def is_num(value):
    try:
        value + 1
    except:
        try:
            float(value)
            return True
        except:
            return False
    else:
        return True

def is_negative(num):
    try:
        return  False if num>=0 else True
    except:
        try:
            return False if float(num)>=0 else True
        except:
            return False


def is_integer(int_str):
    try:
        if is_num(int_str) and not is_negative(int_str):
            if str(int_str).count(".") == 1:
                for x in str(int_str).split(".")[1]:
                    if x != "0":
                        return False
            return True
        else:
            return False
    except:
        return False


def get_result():
    '''Factor=住房公积金缴存信息（zfgjjInfo）中欠缴期数（arrearsmnhs）'''
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        alter_list = format_data("zfgjjInfo")
        if alter_list == [{}]:
            return u'缺失值'
        else:
            if len(alter_list) >= 1:
                factor = []
                for i in alter_list:
                    arrearsmnhs = i.get("arrearsmnhs")
                    if arrearsmnhs not in null_type_list and is_integer(arrearsmnhs):
                            factor.append(int(float(arrearsmnhs)))
                return sum(factor) if len(factor) > 0 else u'缺失值'
            else:
                return 0
    except:
        return u'缺失值'


if __name__ == "__main__":
    # file_obj = open(r'../data/DAS/Past3YearPatentedInventionsNum/FGW_DSJ.json', encoding='utf-8', mode='r')
    file_obj = open(r'FGW_DSJ.json', encoding='utf-8', mode='r')
    content = file_obj.read()
    FGW_DSJ = json.loads(content, strict=False)
    aa = get_result()
    print(aa)

result = get_result()
